﻿// ZoomToScale.js
/// <reference assembly="System.Web.Extensions" name="MicrosoftAjax.js"/>

Type.registerNamespace('ADF.Samples');

ADF.Samples.zoomScaleClass = function (element) {
    ADF.Samples.zoomScaleClass.initializeBase(this, [element]);
    this.DPI = 96;
    this.MapScale = 1.0;
    this.UPP = 1.0;
    this.DegreeFactor = 40030174.00 / 360.00;
    this.FeetFactor = 12;
    this.MeterFactor = 39.37007874; //39.37007874
    this.MapLevels = 1;
    this._zoomToScaleLastScale = 0;
    this._zoomToScalePanel = null;
    this._dropdown = null;
    this._textbox = null;
    this._zoombutton = null;
}
ADF.Samples.zoomScaleClass.prototype = {
    initialize: function () {
        ADF.Samples.zoomScaleClass.callBaseMethod(this, 'initialize');
        var id = this.get_id();
        this._zoomToScalePanel = $get(this.get_id());
        if (screen.deviceXDPI)
            this.DPI = screen.deviceXDPI;
        if (this._zoomToScalePanel != null) {
            if (this._map.get_layers().get_levels() !== null) this.MapLevels = this._map.get_layers().get_levels().length;
            var dropdowndiv = $get(id + "_ZoomToScaleDropdownDiv");
            var ztsTextBox = $get(id + "_ZoomToScaleTextBoxDiv");
            this._dropdown = $get(id + "_ZoomToScaleSelect");
            this._textbox = $get(id + "_ZoomToScaleTextBox");
            this._zoombutton = $get(id + "_ZoomButton");
            if (this.MapLevels > 1) {
                dropdowndiv.style.display = "";
                ztsTextBox.style.display = "none";
                $addHandler(this._dropdown, "change", Function.createDelegate(this, this._zoomToScaleSelectChange));
            } else {
                dropdowndiv.style.display = "none";
                ztsTextBox.style.display = "";
                $addHandler(this._zoombutton, "mousedown", Function.createDelegate(this, this._zoomToScaleTextExecute));

            }
            this._updateZoomToScaleElements();
        }
        if (this._map != null) {
            this._map.add_extentChanged(Function.createDelegate(this, this._updateZoomToScaleElements));
        }
    },
    get_map: function () {
        /// <value name="map" type="Object">Map control associated with the GoToLocation functionality</value>
        return this._map;
    },
    set_map: function (value) {
        this._map = value;
    },
    get_scales: function () {
        /// <value name="scales" type="Object">Set of scale values for a cached resource</value>
        return this._scales;
    },
    set_scales: function (value) {
        this._scales = value;
    },
    get_units: function () {
        /// <value name="units" type="String">Name of map unit type</value>
        return this._units;
    },
    set_units: function (value) {
        this._units = value;
    },

    _updateZoomToScaleElements: function () {
        /// <summary>
        /// Updates the display
        /// </summary>
        var m = this.get_map();
        var mapId = m.get_id();
        var mapwidth = parseInt(m.get_element().clientWidth);
		//alert("mapwidth: "+mapwidth);
		//alert("m.get_extent().get_width(): "+m.get_extent().get_width());
        var unitsperpixel = m.get_extent().get_width() / mapwidth;
        var scale = Math.round(this._getMapScale(unitsperpixel));
        //alert(scale);
        if (this._zoomToScaleLastScale != scale) {
            
            if (this._textbox != null) {
                this._textbox.value = "1:" + scale.localeFormat("N0"); //#,##0
                //alert(this._textbox.value);
            }
            if (this.MapLevels > 1)
                this._populateZoomToScaleDropdown();

            this._zoomToScaleLastScale = scale;
        }
    },

    _getMapScale: function (unitsperpixel) {
        /// <summary>
        /// Gets current map scale
        /// </summary>
        /// <param name="unitsperpixel" type="Number">Number of units per pixel</param>
        var unitsperinch = unitsperpixel * this.DPI;
        var factor = 1;
       // alert("thong" + this._units);
        switch (this._units) {
            case "Feet": factor = this.FeetFactor; break;
            case "Meters": factor = this.MeterFactor;  break;
            case "Inches": factor = 1; break;
            case "DecimalDegrees": break;
            default: factor = this.DegreeFactor * this.MeterFactor; break;
        }
	//	alert("factor: "+factor);
  //      alert(Math.round(factor * unitsperinch)); //đã xóa factor
        return Math.round(factor *unitsperinch);
    },

    _zoomToScaleSelectChange: function () {
        /// <summary>
        /// Handler for dropdown selection - used if cached resource
        /// </summary>
        var m = this.get_map();
        var level = this._dropdown.selectedIndex;
        var mapwidth = parseInt(m.get_element().clientWidth);
        var unitsperpixel = m.get_extent().get_width() / mapwidth;
        var levels = m.get_layers().get_levels();
        var factor = unitsperpixel / levels[level];
        m.zoom(factor);

    },

    _zoomToScaleTextExecute: function () {
        /// <summary>
        /// Handler for Zoom button clicked - used if dynamic resource
        /// </summary>
        if (this._textbox != null) {
            var scale = this._textbox.value;
            if (scale.indexOf("1:") != -1) {
                //scale = scale.replace(/","/g, "");
                scale = scale.substring(2);
            }
            if (isNaN(scale) || scale <= 0)
                alert("Nhập giá trị số");
            else {
                pixelsize = this._getUnitsPerPixel(scale);
                var m = this.get_map();
                var mapwidth = parseInt(m.get_element().clientWidth);
                var unitsperpixel = m.get_extent().get_width() / mapwidth;
                var factor = unitsperpixel / pixelsize;
                m.zoom(factor);
            }
        }
    },

    _populateZoomToScaleDropdown: function () {
        /// <summary>
        /// Updates dropdown list
        /// </summary>
        var m = this.get_map();
        var id = this.get_id();
        var mapwidth = parseInt(m.get_element().clientWidth);
        var unitsperpixel = m.get_extent().get_width() / mapwidth;
        var layers = m.get_layers();
        var scale;
        if (this._dropdown != null) {
            this._dropdown.options.length = 0;
            for (var dd = 0; dd < this._scales.length; dd++) {
                this._dropdown.options[dd] = new Option(this._scales[dd], dd, false);
            }
            var level = layers.getLevelByNearestPixelsize(unitsperpixel);
            this._dropdown.selectedIndex = level;
        }
    },

    _getUnitsPerPixel: function (scale) {
        /// <summary>
        /// Get units per pixel based on scale
        /// </summary>
        /// <param name="scale" type="Number">Scale to be used</param>
        var metersperpixel = 0.0254 / this.DPI;// 0.000265
        var unitsperpixel = metersperpixel * scale;
     //   alert(this._units);
        switch (this._units) {
            case "Feet": unitsperpixel *= 3.28;  break;
            case "DecimalDegrees": unitsperpixel /= (40030174.00 / 360.00);  break;
        }
       
        return unitsperpixel;
    }

}
ADF.Samples.zoomScaleClass.registerClass('ADF.Samples.zoomScaleClass', Sys.UI.Control);

if (typeof (Sys) !== "undefined") { Sys.Application.notifyScriptLoaded(); }

